<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>
<div class="content-wrap">
    <div class="wrapper" style="bottom: 50px;">
        <ul class="nav nav-tabs">
            <li class="active">
                <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/drug/drugPlanExecute/index')">采购计划执行</a>
            </li>
        </ul>
        <section class="panel panel-default">
            <div class="panel-body">
                <div class="table-responsive no-border">
                    <div class="datatable-top">
                        <div class="pull-left">
                            <form:form id="searchForm" modelAttribute="drugBuyPlan"
                                       onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/drug/drugPlanExecute/index');"
                                       method="post" class="form-inline">
                                <div class="form-group">
                                    <label class=" control-label">采购单号：</label>
                                    <form:select path="purchaseNumber" id="purchaseNumber" htmlEscape="false" class="form-control" data-parsley-required="true">
                                        <form:option value=""></form:option>
                                        <form:options items="${his_fns:getBuyPlanNum('2')}" itemLabel="purchaseNumber" itemValue="purchaseNumber" htmlEscape="false" />
                                    </form:select>
                                </div>
                                <div class="form-group">
                                    <label class="control-label">设置供应商：</label>
                                    <form:input path="drugStock.drugInfo.drugSupplierCata.id" id="supplierId" htmlEscape="false" class="form-control" maxlength="64" type="hidden" />
                                    <form:input path="supplierName" id="supplierName" htmlEscape="false" class="form-control" maxlength="64" />
                                </div>
                                <div class="form-group">
                                    <input id="btnSubmit" class="btn btn-primary" type="submit" value="查询" />
                                    <input id="saveBtn" class="btn btn-primary" type="button" onclick="saveDrugPlanExecute()" value="执行" />
                                </div>
                            </form:form>
                        </div>
                    </div>
                    <form id="saveDrugPlanExecute" action="${ctx}/drug/drugPlanExecute/saveDrugPlanExecute" method="post" class="form-inline">
                        <table id="contentTable" class="table table-bordered table-striped mg-t datatable">
                            <thead>
                            <tr>
                                <th hidden>jsonText</th>
                                <th hidden>id</th>
                                <th hidden>stateFlag</th>
                                <th hidden>infoId</th>
                                <th hidden>采购序号</th>
                                <th hidden>采购单号</th>
                                <th>药品编码</th>
                                <th>药品名称</th>
                                <th>包装数量</th>
                                <th>包装规格</th>
                                <th hidden>包装单位代码</th>
                                <th>包装单位</th>
                                <th hidden>剂型</th>
                                <th hidden>厂家ID</th>
                                <th>厂家</th>
                                <th style="width: 25px;">进价</th>
                                <th>计划数量</th>
                                <th>计划金额</th>
                                <th>审核数量</th>
                                <th>申请者</th>
                                <th>采购数量</th>
                                <th>采购金额</th>
                                <th hidden>采购供应商ID</th>
                                <th>采购供应商</th>
                                <%--<th>库存参考数</th>--%>
                                <%--<th>月消耗量</th>--%>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${page.list}" var="drugBuyPlan" varStatus="idx">
                            <tr xia="${idx.index}">
                                <td hidden>${drugBuyPlan.id}<input type="hidden" id="id${idx.index}" name="drugBuyPlanList[${idx.index}].id" value="${drugBuyPlan.id}" /></td>
                                <td hidden>${drugBuyPlan.stateFlag}<input type="hidden" name="drugBuyPlanList[${idx.index}].stateFlag" value="${drugBuyPlan.stateFlag}" /></td>
                                <td hidden> ${drugBuyPlan.infoId}<input type="hidden" id="infoId${idx.index}" name="drugBuyPlanList[${idx.index}].infoId" value="${drugBuyPlan.infoId}" /></td>
                                <td hidden> ${drugBuyPlan.purchaseOrder}<input type="hidden" id="purchaseOrder${idx.index}" name="drugBuyPlanList[${idx.index}].purchaseOrder"
                                                                               value="${drugBuyPlan.purchaseOrder}" /></td>
                                <td hidden> ${drugBuyPlan.purchaseNumber}<input type="hidden" id="purchaseNumber${idx.index}" name="drugBuyPlanList[${idx.index}].purchaseNumber"
                                                                                value="${drugBuyPlan.purchaseNumber}" /></td>
                                <td> ${drugBuyPlan.drugCode}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugCode" value="${drugBuyPlan.drugCode}" /></td>
                                <td> ${drugBuyPlan.drugName}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugName" value="${drugBuyPlan.drugName}" /></td>
                                <td> ${drugBuyPlan.drugStock.drugInfo.packageNum}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugStock.drugInfo.packageNum"
                                                                                        value="${drugBuyPlan.drugStock.drugInfo.packageNum}" /></td>
                                <td> ${drugBuyPlan.packageSpec}<input type="hidden" name="drugBuyPlanList[${idx.index}].packageSpec" value="${drugBuyPlan.packageSpec}" /></td>
                                <td hidden> ${drugBuyPlan.packageUnit}<input type="hidden" name="drugBuyPlanList[${idx.index}].packageUnit" value="${drugBuyPlan.packageUnit}" /></td>
                                <td> ${fns:getDictLabel(drugBuyPlan.packageUnit,'SPEC_UNIT' ,'' )}</td>
                                <td hidden>${drugBuyPlan.drugForm}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugForm" value="${drugBuyPlan.drugForm}" /></td>
                                <td hidden> ${drugBuyPlan.vedorName}<input type="hidden" name="drugBuyPlanList[${idx.index}].vedorName" value="${drugBuyPlan.vedorName}" /></td>
                                <td> ${his_fns:getSupplierById(drugBuyPlan.vedorName)}</td>
                                    <%--进价--%>
                                <td><input name="drugBuyPlanList[${idx.index}].costPrice" id="costPrice${idx.index}" value="${drugBuyPlan.costPrice}" class="form-control"
                                           onchange="changeFun('costPrice','${idx.index}')"></td>
                                <%--计划数量--%>
                                <td> ${drugBuyPlan.plannedSelling}<input type="hidden" name="drugBuyPlanList[${idx.index}].plannedSelling" value="${drugBuyPlan.plannedSelling}" /></td>
                                <%--计划金额--%>
                                <td> ${drugBuyPlan.plannedAmount}<input type="hidden" name="drugBuyPlanList[${idx.index}].plannedAmount" value="${drugBuyPlan.plannedAmount}" /></td>
                                <%--审核数量--%>
                                <td> ${drugBuyPlan.auditSelling}<input type="hidden" name="drugBuyPlanList[${idx.index}].auditSelling" value="${drugBuyPlan.auditSelling}" /></td>
                                <td>${his_fns:getNameByUserId(drugBuyPlan.warehouseKeeper)}
                                    <input type="hidden" name="drugBuyPlanList[${idx.index}].warehouseKeeper" value="${drugBuyPlan.warehouseKeeper}" /></td>
                                    <%--采购数量--%>
                                <td>
                                    <input name="drugBuyPlanList[${idx.index}].purchaseSelling" id="selling${idx.index}" value="${drugBuyPlan.purchaseSelling}" class="form-control" width="20px"
                                           onchange="changeFun('selling','${idx.index}')"></td>
                                    <%--采购金额--%>
                                <td><input name="drugBuyPlanList[${idx.index}].purchaseAmount" id="amount${idx.index}" value="${drugBuyPlan.purchaseAmount}"
                                           class="form-control" width="20px" readonly></td>
                                <td hidden><input type="hidden" name="drugBuyPlanList[${idx.index}].supplierName" id="supplierName${idx.index}"
                                                  class="form-control" width="20px" readonly></td>
                                <td><input id="supplierId${idx.index}" class="form-control" width="20px" value="${drugBuyPlan.supplierName}" readonly></td>
                                <%--<td> ${drugBuyPlan.drugStock.quantity}</td>--%>
                                <%--<td>${drugBuyPlan.monthUsed}</td>--%>
                                <td>
                                    <a href="#" class="applyBt btn btn-danger btn-xs" onclick="removeList(${idx.index},this)">删除</a>
                                </td>
                            </tr>
                            </c:forEach>
                        </table>
                    </form>
                </div>
            </div>
        </section>
    </div>
</div>
<script type="text/javascript">
    //加载供应商
    autoCompleteDrugSupplierCata("supplierName");
    function autoCompleteDrugSupplierCata(inputId) {
        var url = "${ctx}/drug/drugSupplierCata/autoComplete";
        autoComplete(inputId, url,
                function (data) {
                    var rows = [];
                    for (var i = 0; i < data.length; i++) {
                        rows[i] = {
                            data: data[i],
                            value: data[i].id,
                            result: data[i].supplierJc
                        };
                    }
                    return rows;
                }, function (data, i, max) {
                    return data.supplierJc;
                }, function (event, data, formatted) {
                    $("#contentTable tbody tr").each(function () {
                        var xia = $(this).attr("xia");
                        $('#supplierName' + xia).val(data.id);
                        $('#supplierId' + xia).val(data.supplierJc);
                    });
                });
    }
    //保存
    function saveDrugPlanExecute() {
        var purchaseNumber=$("#purchaseNumber").val();
        $("#saveDrugPlanExecute").ajaxSubmit({
            success: function (data) {
                if (data.code == 'success') {
                    toastr.success("采购计划执行成功");
                    layer.open({
                        title: "需要打印单据吗?",
                        type: 1,
                        area: ['300px', '150px'],
                        fixed: false, //不固定
                        skin: 'layui-layer-rim',
                        maxmin: true,
                        content:"",
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            printPdf('${ctx}/drug/drugPlanExecute/drugPlanPrint?purchaseNumber='+purchaseNumber);
                        }, no: function (index) {
                            layer.close(index);
                        }
                    });
                    loadDiv('mainCenterDiv', '${ctx}/drug/drugPlanExecute/index');
                } else {
                    toastr.error(data.data);
                }
            },
            error: function (data) {
                toastr.error(data.data);
            }
        });
    }
    //删除
    function removeList(tbIdx, btn) {
        var purchaseNumber = $('#purchaseNumber' + tbIdx).val();
        if (tbIdx == Number($("#contentTableU tbody tr:last").attr('xia'))) {   //删除的是最后一行

        } else {
            $("#contentTableU tbody tr").each(function (index, item) {    //删除的不是最后一行

            });
        }
        var id = $('#id' + tbIdx).val();
        if (null == id || $.trim(id) == '' || typeof(id) == 'undefined') {
            toastr.success('删除成功!');
            $(btn).parent().parent().remove();
            return;
        }
        $.ajax({
            type: "POST",
            url: '${ctx}/drug/buyPlanAudit/delete?id=' + id,
            success: function (data) {
                if (data.code == "success") {
                    toastr.success(data.data);
                    $(btn).parent().parent().remove();
                } else {
                    toastr.error(data.data);
                }
            },
            error: function (data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }
    //根据输入的采购数量计算采购金额
    function changeFun(inputId, idx) {
        if (inputId == 'selling') {  //改变采购数量
            var num = $.trim($("#" + inputId + idx).val());
            var price = $.trim($("#costPrice" + idx).val());
            $("#amount" + idx).val(Number(num) * Number(price));
        }
        if (inputId == 'costPrice') {  //改变进价
            var price = $.trim($("#" + inputId + idx).val());
            var num = $.trim($("#selling" + idx).val());
            $("#amount" + idx).val(Number(num) * Number(price));
        }
    }
</script>